﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Hyunhdai_Saigon_Project.uc
{
    public partial class uc_sp_timkiem : System.Web.UI.UserControl
    {
        public String sql = "";
        public PagedDataSource pds = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {

            string dx = Request.QueryString.Get("dx");
            int idgx = Convert.ToInt16(Request.QueryString["dgx"]);
            string dgx = "chọn dòng xe";
            if (idgx == 1)
                dgx = "Xe Du Lịch";
            else if (idgx == 2)
                dgx = "Xe Khách";
            else if (idgx == 3)
                dgx = "Xe Tải";

            string hx = Request.QueryString.Get("hx");

            if (hx.Trim().ToLower().Equals("0"))
            {
                if (dgx.Trim().ToLower().Equals("chọn dòng xe"))
                {
                    if (dx.Trim().ToLower().Equals("chọn đời xe"))
                    {
                        sql = "select * from xe";
                    }
                    else
                    {
                        sql = "select * from xe";
                        sql = sql + " where doixe = " + dx;
                    }
                }
                else
                {
                    if (dx.Trim().ToLower().Equals("chọn đời xe"))
                    {
                        sql = "select * from xe";
                        sql += " where dongxe = N'" + dgx + "'";
                    }
                    else
                    {
                        sql = "select * from xe";
                        sql += " where dongxe = N'" + dgx + "'";
                        sql = sql + " and doixe = " + dx;
                    }
                }
            }
            else
            {
                if (dgx.Trim().ToLower().Equals("chọn dòng xe"))
                {
                    if (dx.Trim().ToLower().Equals("chọn đời xe"))
                    {
                        sql = "select * from xe";
                        sql = sql + " where idnsx =" + hx;
                    }
                    else
                    {
                        sql = "select * from xe";
                        sql = sql + " where idnsx =" + hx;
                        sql += " and doixe = " + dx;
                    }
                }
                else
                {
                    if (dx.Trim().ToLower().Equals("chọn đời xe"))
                    {
                        sql = "select * from xe";
                        sql = sql + " where idnsx =" + hx;
                        sql += " and dongxe = N'" + dgx + "'";
                    }
                    else
                    {
                        sql = "select * from xe";
                        sql = sql + " where idnsx =" + hx;
                        sql += " and dongxe = N'" + dgx + "'";
                        sql += " and doixe = " + dx;
                    }
                }
              
            }
            if (sql != null)
            {
                manualBindGrid(sql);
            }
        }

        public void manualBindGrid(String sql)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisg"].ConnectionString);
            connection.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, connection);
            DataTable dt = new DataTable();
            try
            {
                da.Fill(dt);
            }
            catch (Exception)
            {
                lblNoData.Text = sql;
            }
            connection.Close();
            pds.DataSource = dt.DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
            pds.CurrentPageIndex = CurrentPage;
            lnkbtnNext.Enabled = !pds.IsLastPage;
            lnkbtnPrevious.Enabled = !pds.IsFirstPage;

            dlCountry.DataSource = pds;
            dlCountry.DataBind();
            doPaging();
        }
        public int CurrentPage
        {
            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 0;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }
            set
            {
                this.ViewState["CurrentPage"] = value;
            }

        }


        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            for (int i = 0; i < pds.PageCount; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }

            dlPaging.DataSource = dt;
            dlPaging.DataBind();

        }

        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("lnkbtnPaging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                manualBindGrid(sql);
            }
        }
        protected void lnkbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            manualBindGrid(sql);
        }

        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            manualBindGrid(sql);
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentPage = 0;
            manualBindGrid(sql);
        }

        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Font.Bold = true;
            }
        }
        public String addchar(String str)
        {
            return "/" + str;
        }

        protected void ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Image la2 = (Image)e.Item.FindControl("text");
            Label dgx = (Label)e.Item.FindControl("dgx");
            Label dx = (Label)e.Item.FindControl("dx");
            Label nt = (Label)e.Item.FindControl("nt");
            Label sc = (Label)e.Item.FindControl("sc");
            Label scn = (Label)e.Item.FindControl("scn");
            Label hs = (Label)e.Item.FindControl("hs");
            Label nl = (Label)e.Item.FindControl("nl");
            Label htnl = (Label)e.Item.FindControl("htnl");
            Label ttnl = (Label)e.Item.FindControl("ttnl");

            string demo = "<table BORDER=1 CELLPADDING=3 CELLSPACING=1 RULES=ROWS RULES=COLS FRAME=BOX  class = tooltip>" + "<tr><td width = 180px>Dòng Xe:</td><td width:150px>" + dgx.Text + "</td></tr><tr><td>Đời Xe:</td><td>" + dx.Text + "</td></tr><tr><td>Ngoại Thất:</td><td>" + nt.Text + "</td></tr><tr><td>Số Cửa:</td><td>" + sc.Text + "</td></tr><tr><td>Số Chỗ Ngồi:</td><td>" + scn.Text + "</td></tr><tr><td>Hộp Số:</td><td>" + hs.Text + "</td></tr><tr><td>Nhiêu Liệu:</td><td>" + nl.Text + "</td></tr><tr><td>Hệ Thống Nhiên Liệu:</td><td>" + htnl.Text + "</td></tr><tr><td>Tiêu Thụ Nhiên Liệu:</td><td>" + ttnl.Text + "</td></tr></table>";


            String str = dgx.Text + "<br />" + dx.Text + "<br />" + nt.Text + "<br />" + sc.Text + "<br />" + scn.Text + "<br />" + hs.Text + "<br />" + nl.Text + "<br />" + htnl.Text + "<br />" + ttnl.Text;
            la2.Attributes.Add("onMouseover", "ddrivetip(' " + demo + " ', 298,'white');");
            la2.Attributes.Add("onmouseout", "hideddrivetip();");

        }
    }
}